package cn.dulian.spring5.testdemo;

import cn.dulian.spring5.Book;
import cn.dulian.spring5.Orders;
import cn.dulian.spring5.User;
import cn.dulian.spring5.bean.Emp;
import cn.dulian.spring5.service.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author dulian
 * @create 2022-04-14 22:24
 */
public class TestBean {

    @Test
    public void testBean1(){
        //1.加载spring配置文件
        ClassPathXmlApplicationContext bean1 = new ClassPathXmlApplicationContext("bean1.xml");
        //2.获取配置创建的对象
        User bean = bean1.getBean("user",User.class);//不加name也行
        User bean2 = bean1.getBean(User.class);
        System.out.println(bean==bean2);//单例
        bean.add();
    }

    @Test
    public void testBean2(){
        //1.加载spring配置文件
        ClassPathXmlApplicationContext bean1 = new ClassPathXmlApplicationContext("bean1.xml");
        //2.获取配置创建的对象
        Book book = bean1.getBean("book", Book.class);//不加name也行
        System.out.println(book);//单例
        book.testDemo();
    }

    @Test
    public void testBean3(){
        //1.加载spring配置文件
        ClassPathXmlApplicationContext bean1 = new ClassPathXmlApplicationContext("bean1.xml");
        //2.获取配置创建的对象
        Orders orders = bean1.getBean("orders", Orders.class);//不加name也行
        orders.ordersTest();
    }

    @Test
    public void testBean4(){
        //1.加载spring配置文件
        ClassPathXmlApplicationContext bean1 = new ClassPathXmlApplicationContext("bean2.xml");
        //2.获取配置创建的对象
        UserService userService = bean1.getBean("userService", UserService.class);//不加name也行
        userService.add();
    }

    @Test
    public void testBean5(){
        //1.加载spring配置文件
        ClassPathXmlApplicationContext bean1 = new ClassPathXmlApplicationContext("bean3.xml");
        //2.获取配置创建的对象
        Emp emp = bean1.getBean("emp", Emp.class);//不加name也行
        emp.add();
    }

    @Test
    public void testBean6(){
        //1.加载spring配置文件
        ClassPathXmlApplicationContext bean1 = new ClassPathXmlApplicationContext("bean4.xml");
        //2.获取配置创建的对象
        Emp emp = bean1.getBean("emp", Emp.class);//不加name也行
        emp.add();
    }
}
